<template>
  <div class="login-wrapper">
    <div class="shading">
      <uv-image width="180rpx" height="180rpx" :src="applicationConfig.appLogo" mode="widthFix"></uv-image>
    </div>
    <div class="whiteBg">
      <div class="list">
        <form @submit.prevent="submit">
          <div class="item">
            <div class="acea-row row-middle">
              <image src="/static/images/phone_1.png" style="width: 24rpx; height: 34rpx;"/>
              <input type="text" class="texts" placeholder="输入手机号码" v-model="account" required/>
            </div>
          </div>
          <div class="item">
            <div class="acea-row row-middle">
              <image src="/static/images/code_2.png" style="width: 28rpx; height: 32rpx;"></image>
              <input type="password" class="texts" placeholder="填写登录密码" v-model="password" required/>
            </div>
          </div>
        </form>
      </div>
      <!-- 登录方式二：密码登录 -->
      <div class="logon" :style="{backgroundColor:themConfig.theme.color}" @click="submit">登录</div>

    </div>
    <div class="bottom"></div>
  </div>
</template>
<script>
import sendVerifyCode from "@/mixins/SendVerifyCode";
import * as AuthApi from "@/api/member/auth";
import * as UserApi from "@/api/member/user";
import diyJson from '@/config/diy.js';
const BACK_URL = "login_back_url";
const app = getApp();
export default {
  name: "Login",
  computed: {
  },
  mixins: [sendVerifyCode],
  data: function () {
    return {
      themConfig: diyJson.commonStyle,
      applicationConfig: app.globalData.applicationConfig.applicationConfig,
      account: "",
      password: "",
    };
  },
  onLoad() {

  },
  methods: {
    /**
     * 手机 + 密码登录
     */
    async submit() {
      if (!this.account) {
        return this.$util.Tips({
          title: '请填写账号'
        });
      }
      if (!/^[\w\d]{5,16}$/i.test(this.account)) {
        return this.$util.Tips({
          title: '请输入正确的账号'
        });
      }
      if (!this.password) {
        return this.$util.Tips({
          title: '请填写密码'
        });
      }

      uni.showLoading({
        title: '登陆中...',
        mask: true
      });

      // 2. 执行登录
      AuthApi.login({
        username: this.account,
        password: this.password,
        loginType: 4,
      }).then(({data}) => {
        uni.hideLoading();
        console.log("登录结果", data)
        this.$store.commit("LOGIN", {
          'token': data.accessToken
        });
        this.getUserInfo(data);
      }).catch(e => {
        uni.hideLoading();
        this.$util.Tips({
          title: e
        });
      });
    },
    getUserInfo(data) {
      this.$store.commit("SETUID", data.userId);
      this.$store.commit("OPENID", data.miniOpenId||data.mpOpenId);
      UserApi.getUserInfo().then(res => {
        this.$store.commit("UPDATE_USERINFO", res.data);
        // 调回登录前页面
        let backUrl = this.$Cache.get(BACK_URL) || "/pages/index/index";
        if (backUrl.indexOf('/') !== 0) {
          backUrl = '/' + backUrl;
        }
        if (backUrl.indexOf('/pages/users/login/index') === 0) {
          backUrl = '/pages/index/index';
        }
        uni.reLaunch({
          url: backUrl
        });
      })
    },
  }
};
</script>
<style lang="scss" scoped>
page {
  background: #fff;
}

.appLogin {
  margin-top: 60rpx;

  .hds {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24rpx;
    color: #B4B4B4;

    .line {
      width: 68rpx;
      height: 1rpx;
      background: #CCCCCC;
    }

    p {
      margin: 0 20rpx;
    }
  }

  .btn-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 30rpx;

    .btn {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 68rpx;
      height: 68rpx;
      border-radius: 50%;
    }

    .apple-btn {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-left: 30rpx;
      background: #000;
      border-radius: 34rpx;
      font-size: 40rpx;

      .icon-s-pingguo {
        color: #fff;
        font-size: 40rpx;
      }
    }

    .iconfont {
      font-size: 40rpx;
      color: #fff;
    }

    .wx {
      margin-right: 30rpx;
      background-color: #61C64F;
    }

    .mima {
      background-color: #28B3E9;
    }

    .yanzheng {
      background-color: #F89C23;
    }

  }
}

.code img {
  width: 100%;
  height: 100%;
}

.acea-row.row-middle {
  input {
    margin-left: 20rpx;
    display: block;
  }
}

.login-wrapper {
  padding: 30rpx;

  .shading {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;

    /* #ifdef APP-VUE */
    margin-top: 50rpx;
    /* #endif */
    /* #ifndef APP-VUE */

    margin-top: 200rpx;
    /* #endif */
  }

  .whiteBg {
    margin-top: 100rpx;

    .list {
      border-radius: 16rpx;
      overflow: hidden;

      .item {
        border-bottom: 1px solid #F0F0F0;
        background: #fff;

        .row-middle {
          position: relative;
          padding: 16rpx 45rpx;

          .texts {
            flex: 1;
            font-size: 28rpx;
            height: 80rpx;
            line-height: 80rpx;
            display: flex;
            justify-content: center;
            align-items: center;
          }

          input {
            flex: 1;
            font-size: 28rpx;
            height: 80rpx;
            line-height: 80rpx;
            display: flex;
            justify-content: center;
            align-items: center;
          }

          .code {
            position: absolute;
            right: 30rpx;
            top: 50%;
            color: $theme-color;
            font-size: 26rpx;
            transform: translateY(-50%);
          }
        }
      }
    }

    .logon {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 86rpx;
      margin-top: 80rpx;
      border-radius: 120rpx;
      color: #FFFFFF;
      font-size: 30rpx;
    }

    .tips {
      margin: 30rpx;
      text-align: center;
      color: #999;
    }
  }
}
</style>
